package com.ilancuo.datetime;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeTest {
    public static void main(String[] args) {
//        get();
//        set();
//        modify();
//        format();
        parse();
    }

    private static void get() {
        LocalDateTime rightNow = LocalDateTime.now();
        System.out.println("当前时刻：" + rightNow);
        System.out.println("当前年份：" + rightNow.getYear());
        System.out.println("当前月份：" + rightNow.getMonth());
        System.out.println("当前星期：" + rightNow.getDayOfWeek());
        System.out.println("当前日份：" + rightNow.getDayOfMonth());
        System.out.println("当前日份：" + rightNow.getDayOfYear());
        System.out.println("当前时：" + rightNow.getHour());
        System.out.println("当前分：" + rightNow.getMinute());
        System.out.println("当前秒：" + rightNow.getSecond());
    }

    private static void set() {
        LocalDateTime localDateTime = LocalDateTime.of(2019, Month.DECEMBER, 9, 21, 9, 32);
        System.out.println(localDateTime);
    }

    private static void modify() {
        LocalDateTime rightNow = LocalDateTime.now();
        rightNow = rightNow.minusYears(2); //减少2年
        rightNow = rightNow.plusMonths(3); //增加3个月
        rightNow = rightNow.withYear(2008); //直接修改年
        rightNow = rightNow.withHour(13); //直接修改小时
        System.out.println(rightNow);
    }

    private static void format() {
        LocalDateTime rightNow = LocalDateTime.now();
        String result1 = rightNow.format(DateTimeFormatter.ISO_DATE);
        String result2 = rightNow.format(DateTimeFormatter.BASIC_ISO_DATE);
        String result3 = rightNow.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
    }

    private static void parse() {
        LocalDateTime time = LocalDateTime.parse("2002--01--02 11:21",
                DateTimeFormatter.ofPattern("yyyy--MM--dd HH:mm"));
        System.out.println("解析后的时间为：" + time);
    }
}
